package com.lc.luzhengtang.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.bean.CommentGetBean;
import com.lc.luzhengtang.conn.Conn;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/6/30.
 */

public abstract class MyEvaluteAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private List<CommentGetBean.DataBean> evaluateList;

    public AddPicAdapter2 addPicAdapter;
    //图片路径集合
    public List<String> photo_list;
    //图片路径集合
    private List<String> mPath;
    public Map<Integer, List<String>> listMap;
    public Map<Integer, BaseAdapter> adapterMap;

    public MyEvaluteAdapter(Context context, List<CommentGetBean.DataBean> evaluateList) {

        this.context = context;

        this.evaluateList = evaluateList;
        listMap = new HashMap<>();
        adapterMap = new HashMap<>();
    }


    public void notifyDataSetChanged(List<CommentGetBean.DataBean> evaluateList) {

        for (int i = 0; i < evaluateList.size(); i++) {

            photo_list = new ArrayList<>();

            listMap.put(i, photo_list);

        }

        notifyDataSetChanged();

    }


    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        for (int i = 0; i < evaluateList.size(); i++) {
            listMap.put(i, listMap.get(i));
            Log.w("MyEadapter", "listMap.get(i)" + listMap.get(i));
//            adapterMap.get(i).notifyDataSetChanged();
        }

    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public int getCount() {
        return evaluateList.size();
    }

    @Override
    public Object getItem(int position) {
        return evaluateList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        final ViewHolder holder;

        if (convertView == null) {

            convertView = LayoutInflater.from(context).inflate(R.layout.item_my_evaluate, null);

            ScaleScreenHelperFactory.getInstance().loadView((ViewGroup) convertView);


            holder = new ViewHolder();

            holder.img_shop = (ImageView) convertView.findViewById(R.id.img_shop);
            holder.gv_pic = (GridView) convertView.findViewById(R.id.gv_pic);
            holder.img_camera = (ImageView) convertView.findViewById(R.id.img_camera);
            Log.d("9999999999999999999", Conn.HOST3 + evaluateList.get(position).getPicurl());

            Glide.with(context)
                    .load(Conn.HOST3 + evaluateList.get(position).getPicurl()).error(R.mipmap.zhanwei).placeholder(R.mipmap.zhanwei)
                    .into(holder.img_shop);


            addPicAdapter = new AddPicAdapter2(context, listMap.get(position)) {
                @Override
                public void onPhoto() {

                }

                @Override
                public void deletePhoto() {

                }
            };

            adapterMap.put(position,addPicAdapter);

            //刷新接口设置图片路径
//            refreshListener = new MyEvaluteAdapter.RefreshListener() {
//
//                @Override
//                public void setPhoto(List<String> path) {
//                    mPath = path;
//                    photo_list.addAll(path);
//                    holder.gv_pic.setAdapter(addPicAdapter);
//                }
//
//            };


            holder.gv_pic.setAdapter(addPicAdapter);

            holder.gv_pic.setFocusable(false);

            if (listMap.get(position).size() > 0) {

                holder.gv_pic.setVisibility(View.VISIBLE);

            } else {

                holder.gv_pic.setVisibility(View.GONE);

            }

            holder.img_camera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickItem(position);
                }
            });

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();

        }

        return convertView;

    }

    public abstract void onClickItem(int position);

    private class ViewHolder {
        //商品名称，商品单价
        public TextView tv_evaluate_name, tv_evaluate_price;
        //商品图片，相机按钮
        public ImageView img_evaluate, img_camera;

        public EditText et_evaluate;

        public ImageView img_shop;
        public GridView gv_pic;
    }


}
